package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.DetailReceipt;
import com.ruoyi.common.core.domain.entity.HeadReceipt;
import com.ruoyi.common.core.domain.entity.InventoryProduct;
import com.ruoyi.common.core.domain.entity.ReceiptFrom;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.mapper.DetailReceiptMapper;
import com.ruoyi.system.mapper.HeadReceiptMapper;
import com.ruoyi.system.mapper.InventoryMapper;
import com.ruoyi.system.service.InventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 库存单据 业务层处理
 *
 * @author KC
 */
@Service
public class InventoryServiceImpl implements InventoryService {

    private static final Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Autowired
    protected Validator validator;
    @Autowired
    private HeadReceiptMapper HRMapper;
    @Autowired
    private DetailReceiptMapper DRMapper;
    @Autowired
    private InventoryMapper ItMapper;

    /**
     * 库存货品查询
     */
    @Override
    public List<InventoryProduct> selectInventoryProduct(InventoryProduct bo) {
        return ItMapper.selectInventoryProduct(bo);
    }


    /**
     * 库存单据头表查询
     */
    @Override
    public List<HeadReceipt> headReceiptQuery(HeadReceipt bo) {
        return HRMapper.headReceiptQuery(bo);
    }

    /**
     * 库存单据明细查询
     */
    @Override
    public List<DetailReceipt> detailReceiptQuery(DetailReceipt bo) {
        return DRMapper.SelectDetailReceiptQuery(bo);
    }

    /**
     * 货品发生汇总查询
     */
    @Override
    public List<DetailReceipt> inventorySummaryQuery(DetailReceipt bo) {
        return DRMapper.inventorySummaryQuery(bo);
    }

    /**
     * 库存指定单据查询
     */
    @Override
    public HeadReceipt selectHeadReceiptById(String SystematicReceipt) {
        return HRMapper.selectHeadReceiptById(SystematicReceipt);
    }

    /**
     * 新增入库单据明细
     */
    private void addDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        DRMapper.addDetailReceipt(details);
        if (count == 0) {
            ItMapper.addInventory(details);
        } else {
            ItMapper.updateWarehousingInventory(details);
        }
    }

    /**
     * 新增出库单据明细
     */
    private void outboundDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        if (count == 0) {
            throw new RuntimeException("货品明细中有货品库存为0，货品需入库后才能出库！");
        } else {
            DRMapper.addDetailReceipt(details);
            ItMapper.updateRetrievalInventory(details);
        }
    }

    /**
     * 修改库存单据明细
     */
    private void updateDetails(List<DetailReceipt> details) {
        DRMapper.delDetailReceipt(details);
        DRMapper.addDetailReceipt(details);
        ItMapper.updateWarehousingInventory(details);
        ItMapper.updateRetrievalInventory(details);
    }


    /**
     * 库存调拨单据明细
     */
    private void transferDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        DRMapper.addDetailReceipt(details);
        if (count == 0) {
            ItMapper.addInventory(details);
        } else {
            ItMapper.updateWarehousingInventory(details);
            ItMapper.updateRetrievalInventory(details);
        }
    }

    /**
     * 保存其他入库单据信息
     */
    @Override
    public int saveStorageOther(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            addDetails(details);
            return HRMapper.addHeadReceipt(bo);
        } else {
            updateDetails(details);
            return HRMapper.updateHeadReceipt(bo);
        }
    }

    /**
     * 保存其他出库单据信息
     */
    @Override
    public int saveOutboundOther(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            outboundDetails(details);
            return HRMapper.addHeadReceipt(bo);
        } else {
            updateDetails(details);
            return HRMapper.updateHeadReceipt(bo);
        }
    }

    /**
     * 保存库存调拨单据信息
     */
    @Override
    public int saveInventoryTransfer(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            transferDetails(details);
            return HRMapper.addHeadReceipt(bo);
        } else {
            updateDetails(details);
            return HRMapper.updateHeadReceipt(bo);
        }
    }

    /**
     * 保存库存盘点单据信息
     */
    @Override
    public int saveInventoryCounting(ReceiptFrom bo) {
        long count = HRMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            addDetails(details);
            return HRMapper.addHeadReceipt(bo);
        } else {
            updateDetails(details);
            return HRMapper.updateHeadReceipt(bo);
        }
    }
    
    /**
     * 修改库存单据信息
     */
    @Override
    public int updateInventoryReceipt(ReceiptFrom bo) {
        List<DetailReceipt> details = bo.getDetails();
        updateDetails(details);
        return HRMapper.updateHeadReceipt(bo);
    }


    /**
     * 删除库存单据信息
     */
    @Override
    @Transactional
    public int delInventoryReceipt(List<DetailReceipt> bo) {
        /*     删除单据明细      */
        DRMapper.delDetailReceipt(bo);
        /*     入库货品修改      */
        ItMapper.updateWarehousingInventory(bo);
        /*     出库货品修改      */
        ItMapper.updateRetrievalInventory(bo);
        /*     删除单据头表      */
        return HRMapper.delHeadReceipt(bo);
    }

    /**
     * 导入单据头表
     */
    @Override
    public String importHeadData(List<HeadReceipt> HeadDataList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(HeadDataList) || HeadDataList.size() == 0) {
            throw new ServiceException("导入单据头表数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (HeadReceipt bo : HeadDataList) {
            try {
                // 验证是否存在这个系统编号
                HeadReceipt s = HRMapper.selectHeadReceiptById(bo.getSystematicReceipt());
                if (StringUtils.isNull(s)) {
                    BeanValidators.validateWithException(validator, bo);
                    bo.setSystematicReceipt(bo.getSystematicReceipt());
                    bo.setCreateBy(operName);
                    HRMapper.addHeadReceipt(bo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单据头 " + bo.getSystematicReceipt() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, bo);
                    bo.setSystematicReceipt(s.getSystematicReceipt());
                    bo.setUpdateBy(operName);
                    HRMapper.updateHeadReceipt(bo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单据头 " + bo.getSystematicReceipt() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、单据头 " + bo.getSystematicReceipt() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、单据头 " + bo.getSystematicReceipt() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 导入单据明细表
     */
    @Override
    public String importDetailData(List<DetailReceipt> DetailDataList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(DetailDataList) || DetailDataList.size() == 0) {
            throw new ServiceException("导入单据明细表数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DetailReceipt bo : DetailDataList) {
            try {
                // 验证是否存在这个系统编号
                HeadReceipt s = HRMapper.selectHeadReceiptById(bo.getSystematicReceipt());
                if (StringUtils.isNull(s)) {
                    BeanValidators.validateWithException(validator, bo);
                    bo.setSystematicReceipt(bo.getSystematicReceipt());
                    bo.setCreateBy(operName);
                    DRMapper.addImportDetailReceipt(bo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单据头 " + bo.getSystematicReceipt() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, bo);
                    bo.setSystematicReceipt(s.getSystematicReceipt());
                    bo.setUpdateBy(operName);
                    DRMapper.updateImportDetailReceipt(bo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单据头 " + bo.getSystematicReceipt() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、单据头 " + bo.getSystematicReceipt() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、单据头 " + bo.getSystematicReceipt() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
